package cn.gailvlun.gll.presentation.apartment;

import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.markzhai.recyclerview.BaseViewAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.FragmentApartmentManageBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.ListRes;
import cn.gailvlun.gll.net.apartment.room.RoomType;
import cn.gailvlun.gll.presentation.apartment.adapter.ApartmentManageAdapter;
import cn.gailvlun.gll.presentation.base.BaseFragment;
import cn.gailvlun.gll.net.apartment.room.GetRoomListReq;
import cn.gailvlun.gll.net.apartment.room.Room;
import cn.gailvlun.gll.presentation.base.App;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;



public class ApartmentManageFragment extends BaseFragment implements OnRefreshListener, OnLoadmoreListener {

    public static ApartmentManageFragment newInstance(RoomType roomType) {
        ApartmentManageFragment fragment = new ApartmentManageFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("roomType", roomType);
        fragment.setArguments(bundle);
        return fragment;
    }

    private FragmentApartmentManageBinding mBinding;
    private ApartmentManageAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected boolean isLazyLoad() {
        return true;
    }

    private GetRoomListReq mGetRoomListReq = new GetRoomListReq();

    @Override
    protected View initComponent(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_apartment_manage, container, false);

        mBinding.recyclerView.setHasFixedSize(true);

        mAdapter = new ApartmentManageAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);

        mBinding.srl.setOnRefreshListener(this);
        mBinding.srl.setOnLoadmoreListener(this);
        mBinding.srl.setEnableLoadmore(false);

        return mBinding.getRoot();
    }

    @Override
    protected void createHandler() {
    }

    private RoomType mRoomType;

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        mRoomType = (RoomType) getArguments().getSerializable("roomType");
        mGetRoomListReq.setType(mRoomType);

        mBinding.srl.autoRefresh(0);
    }

    public void autoRefresh() {
        mBinding.srl.autoRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mGetRoomListReq.resetPage();
        HttpMethods.getRoomService()
                .getRoomList(mGetRoomListReq.getDirection(), mGetRoomListReq.getId(),
                        mGetRoomListReq.getType(), mGetRoomListReq.getRoom_tag() == null ? null : mGetRoomListReq.getRoom_tag().name())
                .compose(RxUtil.<ListRes<Room>>applyScheduler())
                .compose(this.<ListRes<Room>>bindToLifecycle())
                .subscribe(new HttpSubscriber<ListRes<Room>>() {
                    @Override
                    protected void onSuccess(ListRes<Room> response) {
                        mAdapter.set(response.getResults());
                        mBinding.srl.setEnableLoadmore(response.isNext());
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable ListRes<Room> response, int code) {
                        DialogUtil.showError(mContext, errMsg);
                    }

                    @Override
                    public void onComplete() {
                        mBinding.srl.finishRefresh();
                    }
                });
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mGetRoomListReq.nextPage(mAdapter.getData().get(mAdapter.getData().size() - 1).getId());
        HttpMethods.getRoomService()
                .getRoomList(mGetRoomListReq.getDirection(), mGetRoomListReq.getId(),
                        mGetRoomListReq.getType(), mGetRoomListReq.getRoom_tag() == null ? null : mGetRoomListReq.getRoom_tag().name())
                .compose(RxUtil.<ListRes<Room>>applyScheduler())
                .compose(this.<ListRes<Room>>bindToLifecycle())
                .subscribe(new HttpSubscriber<ListRes<Room>>() {
                    @Override
                    protected void onSuccess(ListRes<Room> response) {
                        mAdapter.addAll(response.getResults());
                        mBinding.srl.setEnableLoadmore(response.isNext());
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable ListRes<Room> response, int code) {
                        DialogUtil.showError(mContext, errMsg);
                    }

                    @Override
                    public void onComplete() {
                        mBinding.srl.finishLoadmore();
                    }
                });
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onClickRoom(Room room) {
            ApartmentDetailActivity.openActivity(mContext, room.getId());
        }
    }

    public static class Util {

        public static Drawable getStatusBackground(Room room) {
            switch (room.getStatus()) {
                case UN_REVIEW:
                    return App.getInstance().getResources().getDrawable(R.drawable.rounded_bluey_grey);
                case REVIEW_OBJECTION:
                    return App.getInstance().getResources().getDrawable(R.drawable.rounded_salmon);
                case REVIEW_PASS:
                    return App.getInstance().getResources().getDrawable(R.drawable.rounded_robinsegg);
                case SIGNING:
                    return App.getInstance().getResources().getDrawable(R.drawable.rounded_apricot);
                case SIGNING_COMPLETE:
                    return App.getInstance().getResources().getDrawable(R.drawable.rounded_pinkish_grey_two);





                default:
                    return App.getInstance().getResources().getDrawable(R.drawable.rounded_hospital_green);
            }
        }

        public static SpannableString getPeople(Room room) {
            SpannableStringBuilder ssb = new SpannableStringBuilder()
                    .append(String.valueOf(room.getMember_count()))
                    .append("人参与");
            ssb.setSpan(
                    new ForegroundColorSpan(
                            App.getInstance().getResources().getColor(R.color.apricot)),
                    0,
                    String.valueOf(room.getMember_count()).length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            );
            return SpannableString.valueOf(ssb);
        }
    }
}
